﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace GeracaoArquivoCorretora
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        public DataTable dtEmpresas = new DataTable();
        public bool Criptografada;
        public bool Continue = false;
        public string database;
        public string usuario;
        public string senha;

        [DllImport("Sinacor.dll")]
        public static extern string GetSenha(int numero);

        private void btnContinue_Click(object sender, EventArgs e)
        {
            if(cboDataBase.SelectedItem != null)
            {

                string connection_string = "Data Source=" + cboDataBase.SelectedItem.ToString() + ";User Id=" + usuario + ";Password=" + senha + ";";



                //abrir conexao oracle
                System.Data.OracleClient.OracleDataAdapter adapter = new System.Data.OracleClient.OracleDataAdapter("select * from tgeempr", connection_string);
                adapter.Fill(dtEmpresas);
                adapter.Dispose();

                this.database = cboDataBase.SelectedItem.ToString();

                Continue = true;
                this.Close();
            }
            else
            {
                MessageBox.Show("Todos os campos são obrigatórios");
            }
            
        }

        private void Login_FormClosing(object sender, FormClosingEventArgs e)
        {
            if(!Continue)
                Application.Exit();
        }

        private void Login_Load(object sender, EventArgs e)
        {
            //ler o arquivo sinacor.ini


            string[] linhas = System.IO.File.ReadAllLines("sinacor.ini");

            string usuarioSinacor = string.Empty;
            string caminhoSqlIni = string.Empty;

            foreach(string linha in linhas)
            {
                if(linha.IndexOf("UsuarioSinacor") >= 0)
                {
                    usuarioSinacor = linha.Split('=')[1].ToString().Trim().Replace(";","");
                }

                if(linha.IndexOf("CaminhoSqlIni") >= 0)
                {
                    caminhoSqlIni = linha.Split('=')[1].ToString().Trim().Replace(";", "");
                }
            }

            string[] linhas_banco = System.IO.File.ReadAllLines(caminhoSqlIni + "\\sql.ini");

            foreach(string linha_banco in linhas_banco)
            {
                if(linha_banco.IndexOf("remotedbname") >= 0)
                {
                    string banco = linha_banco.Split(new char[] { '=', ',' })[1];
                    cboDataBase.Items.Add(banco);
                }

            }

            usuario = usuarioSinacor;
            senha = GetSenha(0);

            
                
        }
    }
}
